// 仓库文件
import { defineStore } from "pinia";

// 第二个参数支持两种风格：options api 以及 composition api,此处是options api
// pinia 中取消了原有的mutations,保留了actions，同步异步操作都在actions中，
// 选项式操作类似于vuex操作
// defineStore 定义store
export const useCounterStore = defineStore('counter', {
    state: () => {
        return {
            count: 0
        }
    },
    getters: {
        // pinia中的计算属性
        doubleCount(state) {
            return this.count * 2;
        }
    },
    actions: {
        increment() {
            this.count++;
        },
        decrement() {
            if (this.count === 0) {
                return
            }
            this.count--;
        },
        async asyncIncrement() {
            await new Promise((resolve) => setTimeout(resolve, 1000))
            // 等待一秒钟
            this.increment()
        },
        async asyncDecrement() {
            await new Promise((resolve) => setTimeout(resolve, 1000));
            this.decrement();
        },
    }
})